﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using IceCS.DataStructure.Core.Queue;
using IceCS.DataStructure.Helpers;

namespace IceCS.DataStructure.BankCall
{
    public class BankCallSystem: SystemBase
    {
        public IBankQueue BankQueue;
        public BankCallSystem()
        {

        }

        /// <summary>
        /// 初始化存储结构
        /// </summary>
        protected override void InitStorageStructure()
        {
            var tips = "请选择存储结构的类型：\n1.顺序队列\n2.链式队列";
            var input = ConsoleHelper.GetRightInputNumber(tips, 1,2);
            switch (input)
            {
                case 1:
                    int maxCount = ConsoleHelper.GetRightInputNumberGreatThanMinValue("请输入队列人数(不小于 3):", 3);
                    BankQueue = new SequenceBankQueue(maxCount);
                    break;
                case 2:
                    BankQueue = new LinkedBankQueue();
                    break;
            }
        }

        protected override void MainLoop()
        {
            int windowcount = 1;
            var sw = new BankCallService[windowcount];
            var swt = new Thread[windowcount];
            for (int i = 0; i < windowcount; i++)
            {
                sw[i] = new BankCallService(BankQueue);
                sw[i].BankQueue = BankQueue;
                swt[i] = new Thread(start: new ThreadStart(sw[i].Service));
                swt[i].Name = "" + (i + 1);
                swt[i].Start();
            }
            while (true)
            {
                Console.WriteLine("请点击触摸屏获取号码");
                Console.ReadLine();
                int callnumber;
                if(!BankQueue.IsFull){
                    callnumber = BankQueue.GetCallNumber();
                    Console.WriteLine("您的号码是：{0}，您前面有{1}位，请耐心等待。",callnumber,BankQueue.Count);
                    BankQueue.Enqueue(callnumber);
                }
                else
                {
                    Console.WriteLine("现在业务繁忙，请稍后再来。");
                }
                Console.WriteLine();
            }
        }
    }
}
